package com.tbit.uqbike.object.pojo;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

/**
 * 平台服务费催缴数据传输实体类
 *
 * @Author:BUGTian
 * @DATE: 2022/7/16
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RushPayFeeConfig {

    private static final String FLAG_KEY = "flag";
    private static final String CONTACT_TIME_KEY = "contactTime";
    private static final String STOP_USING_TIME = "stopUsingTime";
    private static final String USAGE_FLAG_KEY = "usageFlag";
    private static final String SIM_EXPIRATION_TIME = "simExpirationTime";
    private static final String API_COUNT_KEY = "apiCount";

    /**
     * 开关 0：关，1：开
     */
    private Integer flag;
    /**
     * 联系时间
     */
    private Date contactTime;
    /**
     * 暂停使用时间
     */
    private Date stopUsingTime;

    /**
     * 用量额度提醒开关 0：关，1：开
     */
    private Integer usageFlag;
    /**
     * sim卡过期时间
     */
    private Integer simExpirationTime;
    /**
     * 优驱api使用次数
     */
    private Integer apiCount;

    public static void main(String[] args) {
        Date stopUsingTime = DateUtil.offsetDay(new Date(), 7);
        DateTime contactTime = DateUtil.yesterday();
        RushPayFeeConfig rushPayFeeConfig = new RushPayFeeConfig(1, contactTime, stopUsingTime, 1, 7, 100);
        //转换为key=value形式
        String rushPayFeeConfigStr = rushPayFeeConfig.toRushPayFeeConfigStr();
        System.out.println(rushPayFeeConfigStr);
        //转换为对象
        RushPayFeeConfig rushPayFeeConfigByStr = new RushPayFeeConfig().getRushPayFeeConfigByStr(rushPayFeeConfigStr);
        System.out.println(rushPayFeeConfigByStr);
    }

    /**
     * 转成key=value形式保存到配置表
     *
     * @return
     */
    public String toRushPayFeeConfigStr() {
        return String.format("%s=%s&%s=%s&%s=%s&%s=%s&%s=%s&%s=%s",
                FLAG_KEY, flag == null ? 0 : flag,
                CONTACT_TIME_KEY, contactTime == null ? 0 : contactTime.getTime(),
                STOP_USING_TIME, stopUsingTime == null ? 0 : stopUsingTime.getTime(),
                USAGE_FLAG_KEY, usageFlag == null ? 0 : usageFlag,
                SIM_EXPIRATION_TIME, simExpirationTime == null ? 0 : simExpirationTime,
                API_COUNT_KEY, apiCount == null ? 0 : apiCount
        );
    }

    /**
     * 根据key=value形式的字符串，转换为实体类
     *
     * @param bmsConfigStr
     * @return
     */
    public RushPayFeeConfig getRushPayFeeConfigByStr(String bmsConfigStr) {
        String[] items = bmsConfigStr.split("&");
        setFlag(Integer.parseInt(items[0].split("=")[1]));
        setContactTime(new Date(Long.parseLong(items[1].split("=")[1])));
        setStopUsingTime(new Date(Long.parseLong(items[2].split("=")[1])));
        setUsageFlag(Integer.parseInt(items[3].split("=")[1]));
        setSimExpirationTime(Integer.parseInt(items[4].split("=")[1]));
        setApiCount(Integer.parseInt(items[5].split("=")[1]));
        return this;
    }
}
